(function()
{
	var langFun = function(lan, fun)
	{
		this.lang = null;
		this.data = null;
		this.setLan(lan, fun);
		this.updateWordIfNo = true; // 不存在单词，上报
		this.updateNameList = [];
	};
	langFun.prototype.addUpdateWord = function(name)
	{
		// if (!login.haveAuth('login', 'U') || !config.debug)
		// return;
		if (login.getUserName() == 'root')
			return;
		var url = ajax.getUrl('wtp.doc.lang.add');
		if (!url)
			return;
		var that = this;
		if (!this.updateList)
			this.updateList = [];
		if (arguments.length <= 0)
		{
			if (this.updateList.length <= 0 || !window.app || !app.haveInit)
				return;
			if (this.addUpdateWord.index)
			{
				clearTimeout(this.addUpdateWord.index);
				this.addUpdateWord.index = 0;
			}
			ajax.setBackRequest(true);
			ajax.request('wtp.doc.lang.add', {
				words : this.updateList,
				core : app.core,
				adminDir : config.wtpAdminDir,
			}, function(d, ad, msg, code)
			{
			}, function(msg, msg2)
			{
				// showMsg('addUpdateWord : ' + msg2);
			});
			this.updateList = [];
			return;
		}
		if (this.updateNameList.indexOf(name) >= 0)
			return;
		else
			this.updateNameList.push(name);
		this.updateList.push({
			lan : this.lang,
			name : name,
			value : ''
		});
		if (!this.addUpdateWord.index)
		{
			this.addUpdateWord.index = setTimeout(function()
			{
				that.addUpdateWord();
			}, 10000);
		}
	}
	// 设置当前语言
	langFun.prototype.setLan = function(lan, fun)
	{
		if (lan == 'none' || !lan)
		{
			if (fun)
				fun();
		}
		else
		{
			configPara.setLan(lan);
			if (this.data == null)
			{
				this.data = {};
				// 调入login语言包
				loadJs('lang/servers/login/' + lan + '.js', function()
				{
					if (fun)
						fun();
				});
			}
			else if (fun)
				fun();
		}
		this.lang = lan;
	}
	langFun.prototype.getWord = function(key)
	{
		if (!key)
			return null;
		var a = key.split('.');
		var d = this.data;
		if (!d)
		{
			return null;
		}
		for (var i = 0; i < a.length; i++)
		{
			var aa = (a[i]).toLowerCase();
			if (i == 0 && aa == 'all' && a.length > 1)
				continue;
			if (d[aa])
			{
				d = d[aa];
			}
		}
		return d;
	};
	/**
	 * updateWord 准备上传的单词
	 */
	langFun.prototype.w = function(word, updateWord)
	{
		if (!word)
			return '';
		// 去掉括号前后和中间空格
		word = word.replace(/[\s]{0,10}\([\s]{0,10}\)[\s]{0,10}/g, '()').trim();
		// 多个空格替换为一个空格
		word = word.replace(/\s{1,32}/g, ' ');
		var a = word.split('.');
		if (a.length == 1)
		{
			return this.w('login.f.' + word);
		}
		else if (a.length == 2)
		{
			return word;
		}
		var d = this.data;
		if (!d)
		{
			// 无语言的时候原样返回
			return word;
		}
		var fy = false;
		for (var i = 0; i < a.length; i++)
		{
			if (i == 1 && a[i] == 'fy')
				fy = true;
			if (fy)
			{
				// 去掉单独首或尾的括号，如果中间还有就不去掉
				if (a[i].indexOf('(') >= 0)
				{
					var ol = a[i];
					if (ol.indexOf('()') == 0)
						ol = ol.substring(2);
					if (ol.indexOf('()') == ol.length - 2)
						ol = ol.substring(0, ol.length - 2);
					if (ol.indexOf('()') < 0)
						a[i] = ol;
				}
			}
			var aa = a[i].toLowerCase();
			if (d[aa])
			{
				d = d[aa];
				if (typeof d != 'object' || d === undefined)
					break;
			}
			else
			{
				d = undefined;
				break;
			}
		}
		if (typeof d == 'string')
		{
			// 如果值为‘-’，且为f，msg或者fy时 ，将替换到login服务。
			if (d == '-' && (a[1] == 'fy' || a[1] == 'f' || a[1] == 'msg' || fy) && a[0] != 'login')
			{
				return this.w(word.replace(/^[a-z]{2,16}./, 'login.'), word);
			}
			result = ' ' + d + ' ';
		}
		else if (typeof d == 'object')
		{
			// 返回一个数组
			return d;
		}
		else
		{
			if ((a[1] == 'fy' || a[1] == 'f' || a[1] == 'msg' || fy) && a[0] != 'login')
			{
				return this.w(word.replace(/^[a-z]{2,16}./, 'login.'), word);
			}
			if (updateWord === undefined)
				updateWord = word;
			// 失败了，上报（仅仅针对相对短的语句）
			if (updateWord != '' && this.updateWordIfNo && this.data && this.data['login'])
			{
				if (!fy || (a[2] + 'k').indexOf(' ') > 0)
					this.addUpdateWord(updateWord);
			}
			var w = word.split('.');
			w = w[w.length - 1];
			w = fy ? w : this.titleCase(w);
			return ' ' + w + ' ';
		}
		return result;
	};
	/**
	 * 按大小写分节
	 */
	langFun.prototype.titleCase = function(word)
	{
		var s = '';
		var prek = false;
		for (var i = 0; i < word.length; i++)
		{
			var c = word.charAt(i);
			if (c == ' ')
			{
				prek = true;
				continue;
			}
			if (i == 0)
				s += c.toUpperCase();
			else if ((c >= 'A' && c <= 'Z') || prek)
			{
				prek = false;
				if (i < word.length - 1)
				{
					var c2 = word.charAt(i - 1);
					if (c2 < 'A' || c2 > 'Z')
						s += ' ' + c.toUpperCase();
					else
						s += c;
				}
				else
					s += c;
			}
			else
				s += c;
		}
		return s;
	}
	window.langFun = langFun;
}());
